package com.jiuzhou.surery.db;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Repository;

import com.jiuzhou.surery.beans.User;


@Repository
public class UserRepository {
    private final JdbcTemplate jdbcTemplate;

    public UserRepository(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }



    public User findByUsername(String username) {
        return jdbcTemplate.queryForObject(
                "select * from surery_user where username = ?",
                new UserRowMapper(),
                username
        );
    }

    public void deleteByUsername(String username) {
        jdbcTemplate.update(
                "delete from surery_user where username = ?",
                username
        );
    }


    public void save(User user) {
        jdbcTemplate.update(
                "insert into surery_user (username, password,enabled, firecar) values (?, ?, ?, ?)",
                user.getUsername(),
                user.getPassword(),
                user.isEnabled(),
                user.getFirecar()
        );
    }

    public void update(User user) {
        jdbcTemplate.update(
                "update surery_user set password = ?, role = ?, firecar = ? where username = ?",
                user.getPassword(),
                user.isEnabled(),
                user.getFirecar(),
                user.getUsername()
        );
    }

    public boolean existsByUsername(String username) {
        String sql = "select count(*) from surery_user where username = ?";
        return jdbcTemplate.queryForObject(sql, Integer.class, username) > 0;
    }

    public void addAuthority(String username,String authority) {
        jdbcTemplate.update(
                "insert into authorities (username, authority) values (?, ?)",
                username,
                authority
        );
    }
    public void addAuthorities(String username, Collection<? extends GrantedAuthority> authorities) {
       jdbcTemplate.batchUpdate(
                "insert into authorities (username, authority) values (?, ?)",
                authorities.stream()
                        .map(authority -> new Object[]{username, authority.getAuthority()})
                        .collect(Collectors.toList())
        );
    }
 public List<GrantedAuthority> getAuthorities(String username) {
        String sql = "select authority from authorities where username = ?";
        return jdbcTemplate.query(
                sql,(rs,rowNum) -> new SimpleGrantedAuthority(rs.getString("authority")),
                username
        );
    }
    public void deleteAuthority(String username) {
        jdbcTemplate.update(
                "delete from authorities where username = ?",
                username
        );
    }

}
